<?php

require_once WP_PLUGIN_DIR . '/diapucp-events/defines.php' ;
require_once DPEM_SMB_PATH . '/smb_basefield.php' ;

class smartmeta_checkboxenabler extends smb_basefieldbox {
   public function  __construct($fields){
        $this->set_fields($fields);
        $this->set_inputid( $fields['id'] );

        add_action( 'admin_head-post.php', array($this, 'call_js') );
        add_action( 'admin_head-post-new.php', array($this, 'call_js') );
   }

   public static function build_me( $fields, &$new_field_object ){
        $new_field_object = new smartmeta_checkboxenabler($fields);
   }

   public function show(){
        $value = $this->fetch_value();
        ?>
            <td>
                <label for="<?php echo $this->inputid; ?>"><?php echo $this->fields['name']; ?></label>
            </td>
            <td><em>&nbsp;&nbsp;</em>
                <input type="checkbox"
                       name="<?php echo $this->inputid; ?>"
                       id="<?php echo $this->inputid; ?>"
                       <?php checked($value, 'true'); ?>
                       value="true"
                />
            </td>
        <?php
   }

   public function save($post_id){
       if ( !isset( $_POST[$this->inputid] ) ){
           smb_basefieldbox::set($this->fields['id'], 'false', $post_id);
       } else {
           smb_basefieldbox::set($this->fields['id'], 'true',$post_id);
       }
   }

      public function call_js(){
        if ( !smb_basefieldbox::check_page( 'post' ) ) return;

        foreach( $this->fields['enables'] as $item_enable ){
                echo '<script type="text/javascript">';
                    echo 'jQuery(document).submit(function(){';
                    
                    echo '});';
                echo '</script>';
        }
    }

    public function validate_empty_js($field, $order, $type){
        echo 'if(!jQuery("#' . $field . '").val()){';
        echo 'alert("ERROR EMPTY ' . $type . ' ' . $order . '");';
        echo smb_validation::$var_val_name . '=' . smb_validation::$var_val_name . '&&false;};';
    }

    public function compare_js($field1, $field2, $type, $comparesign){
        echo 'if(jQuery("#'. $field1 .'").val()'. $comparesign . 'jQuery("#'. $field2 . '").val()){';
        echo 'alert("ERROR '. $type .' COMPARE");';
        echo smb_validation::$var_val_name . '=' . smb_validation::$var_val_name . '&&false;};';
    }
}

?>